﻿using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Transform 类扩展
/// </summary>
public static class TransformExtension
{

    /// <summary>
    /// 查找 Transform 下指定路径的组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="transform"></param>
    /// <param name="path">Transform 下指定路径</param>
    /// <returns></returns>
    public static T GetComponent<T>(this Transform transform, string path) where T : Component
    {
        return Utility.Component.FindGameComponent<T>(transform, path);
    }

    /// <summary>
    /// 查找 GameObject 下指定路径的组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="gameObject"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static T FindComponent<T>(this Transform transform, string path) where T : Component
    {
        return Utility.Component.FindGameComponent<T>(transform, path);
    }

    /// <summary>
    /// 获取或者添加组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="transform"></param>
    /// <returns></returns>
    public static T GetOrAddComponent<T>(this Transform transform) where T : Component
    {
        return Utility.Component.GetOrAddComponent<T>(transform.gameObject);
    }

    /// <summary>
    /// 获取或者添加 Transform 下指定路径的组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="transform"></param>
    /// <param name="path">Transform 下指定路径</param>
    /// <returns></returns>
    public static T GetOrAddComponent<T>(this Transform transform, string path) where T : Component
    {
        return Utility.Component.GetOrAddComponent<T>(transform, path);
    }

    /// <summary>
    /// 通过名字查找对应的组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="transform"></param>
    /// <param name="childName"></param>
    /// <returns></returns>
    public static T GetChildComponentByName<T>(this Transform transform, string childName)
    {
        Transform child = transform.GetChildByName(childName);
        if (child == null)
            return default;
        return transform.GetComponent<T>();
    }

    /// <summary>
    /// 通过名字获取子节点
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="childName"></param>
    /// <returns></returns>
    public static Transform GetChildByName(this Transform transform, string childName)
    {
        Transform[] allChilds = transform.GetComponentsInChildren<Transform>(true);
        foreach (Transform child in allChilds)
        {
            if (child.name.Equals(childName))
            {
                return child;
            }
        }
        return null;
    }

    /// <summary>
    /// 设置节点置灰
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="isGray"></param>
    /// <returns></returns>
    public static void SetGray(this Transform transform, bool isGray)
    {
      
    }

    public static void DestroyChilds(this Transform transform)
    {
        for (int i = transform.childCount - 1; i >= 0; i--)
        {
            Object.Destroy(transform.GetChild(i).gameObject);
        }
    }

    public static void SetParticleSystemSpeed(this Transform transform, float speed)
    {
        var array = transform.GetComponentsInChildren<ParticleSystem>(true);
        for (int i = 0, cnt = array.Length; i < cnt; i++)
        {
            var ps = array[i];
            var main = ps.main;
            main.simulationSpeed = speed;
        }
    }

    public static void SetAnimationSpeed(this Transform transform, float speed)
    {
        var array = transform.GetComponentsInChildren<Animator>(true);
        for (int i = 0, cnt = array.Length; i < cnt; i++)
        {
            var ps = array[i];
            ps.speed = speed;
        }
    }

    static System.Text.StringBuilder sb = new System.Text.StringBuilder();
    /// <summary>
    /// 查看 transform 路径
    /// </summary>
    /// <param name="transform"></param>
    /// <returns></returns>
    public static string GetPath(this Transform transform, System.Func<Transform, bool> found = null)
    {
        if (transform == null)
            return "null";
        sb.Clear();
        var cur = transform;
        while (cur)
        {
            sb.Insert(0, cur.name + "/");
            if (found != null && found(cur))
                break;
            cur = cur.parent;
        }
        return sb.ToString();
    }
}
